﻿package com.tvein.sanguo.swc.action.role
{
	//flash
	import flash.display.MovieClip;
	import flash.display.Loader;
	import flash.net.URLRequest;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	//A3D
	//custom
	import com.tvein.sanguo.swc.action.role.RoleChoice;
	import com.tvein.sanguo.swc.common.SwcEvent;
	import com.tvein.sanguo.swc.common.MP3Click;
//
	public class RoleList extends MovieClip
	{
		public static const NAME:String = "Role-List";
		
		private var sourceServer:String;
		private var effectVol:int = 100;
		
		public function RoleList()
		{
			stage?init(null):addEventListener(Event.ADDED_TO_STAGE, init);
		}

		private function init(evt:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE , init);
			addEventListener(Event.REMOVED_FROM_STAGE, _onRemoved);
			stage.addEventListener(Event.RESIZE, _resizeUI);
			
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			
			_resizeUI();
		}

		public function console(Type:String, params:Object):void
		{
			switch (Type)
			{
				case "setSource":
					sourceServer = params.Address;
					break;
				case "setEffect":
					effectVol = params.Volume;
					break;
				case "setRoleList":
					setRoleList(params);
					break;
			}
		}
		
		private function setRoleList(params:Object):void
		{
			var roleChoice:RoleChoice;
			
			for (var i = 0; i < params.Process.length; i++)
			{
				roleChoice = new RoleChoice(params.Process[i], effectVol, sourceServer);
				roleChoice.x = 35;
				roleChoice.y = 35 + 180 * i;
					
				this.addChild(roleChoice);
			}
			
			if (i < (params.Main.rank - 1))
			{				
				var nullRole:Object = new Object();
				
				nullRole["Role_ID"] = 0;
				
				roleChoice = new RoleChoice(nullRole, effectVol, sourceServer);
				roleChoice.x = 35;
				roleChoice.y = 35 + 180 * i;
				
				this.addChild(roleChoice);
				
				i = i + 1;
			}
			
			mcFrame.reSize(400, 180 * (i-1) + 230);
			
			_resizeUI();
		}
		
		private function _onRemoved(evt:Event):void
		{
			removeEventListener(Event.REMOVED_FROM_STAGE, _onRemoved);
			stage.removeEventListener(Event.RESIZE, _resizeUI);
		}

		private function _resizeUI(evt:Event = null):void
		{			
			this.x = (stage.stageWidth - this.width) * .5;
			
			if ((stage.stageHeight - this.height) > 100)
			{
				this.y = (stage.stageHeight - this.height) * .5;
			}
			else
			{
				this.y = 50;
			}
		}
	}
}